<?php
$this->breadcrumbs = array(
    Yii::t('main', 'Product Admin')=>array('/admin/product/admin'),
    Yii::t('main', 'Product Quantity Admin')
);


$this->menu = array(
    array('label'=>Yii::t('main', 'Product Admin'), 'url'=>array('/admin/product/admin'))
);
?>

<?php //echo CHtml::link('Thêm mới', '#', array('class' => 'create-button')); ?>
<div class="create-form" style="display:none">
    <?php
    /*
    $this->renderPartial('_form', array(
        'model' => $model,
        'listData' => $listData
    ));*/
    ?>
</div>
<?php
$this->widget('zii.widgets.grid.CGridView', array(
    'id' => 'product-grid',
    'dataProvider' => $model->search(),
    'afterAjaxUpdate'=>'afterGridUpdate',
    'filter' => $model,
    'columns' => array(
        /*
        array(
            'id' => 'autoId',
            'class' => 'CCheckBoxColumn',
            'selectableRows' => '50',
        ),*/
        array(
            'header'=>'',
            'value'=>'$this->grid->dataProvider->pagination->currentPage * $this->grid->dataProvider->pagination->pageSize + ($row+1)'
        ),
        array(
            'name' => 'avatar',
            'value' => '"<img src=\"".($data->avatar?Yii::app()->baseUrl."/".$data->avatar->url:"")."\" width=\"50\" height=\"50\"/>"',
            'type' => 'raw',
            'filter' => FALSE,
            'sortable' => FALSE,
        ),
        array(
            'name' => 'categoryId',
            'value' => 'isset($data->category)?$data->category->name:""',
            'filter' => CHtml::activeDropDownList($model, 'categoryId', $listData),
        ),
        array(
            'name' => 'name',
            'value' => '$data->name',
            'type' => 'raw',
        ),    
        array(
            'name'=>'code'
        ),
        array(
            'name' => 'brandId',
            'value' => '$data->brandId?$data->brand->name:""',
            'filter' => CHtml::activeDropDownList($model, 'brandId', ProductBrand::model()->getListIdName()),
        ),
        array(
            'name' => 'origin',
            'value' => '$data->origin',
            'type' => 'raw',
        ),
        array(
            'name' => 'price',
            'value' => '$data->price . "/" . $data->priceUnit',
            'type' => 'raw',
            'htmlOptions' => array("width" => "50px"),
        ),
        array(
            'name' => 'status',
            'value' => '$data->status==1?"Hiện":"Ẩn"',
            'filter' => array(1 => 'Hiện', 0 => 'Ẩn'),
        ),
        array(
            'header'=>Yii::t('main', 'Quantity'),
            'type'=>'raw',
            'value'=>'"<span id=\"quantity_".$data->id."\">".$data->quantity."</span>"'
        ),
        array(            
            'header'=>Yii::t('main', 'Delta Quantity'),
            'type'=>'raw',
            'value'=>'CHtml::textField("deltaQuantity[".$data->id."]", "0", array("id"=>"p_".$data->id, "style"=>"width:50px"))'
        ),
        array(
            'header'=>'',
            'type'=>'raw',
            'value'=>'CHtml::button(Yii::t("main", "Update Quantity"), array("id"=>$data->id, "class"=>"btnupdate"))'
        ),
    ),
));
?>
<script>
    $(function(){
        initButton();
    });
    
    function initButton(){
        $('.btnupdate').click(function(){            
            var id=$(this).attr('id');
            var delta=$('#p_'+id).val();
            if(delta===0 || delta===''){
                return;
            }
            
            $.ajax({
                url:'<?php echo Yii::app()->createUrl('/admin/product/updateProductQuantity') ?>',
                type:'post',
                dataType:'json',
                data:{
                    'id':id,
                    'delta':delta
                },
                success:function(data){
                    if(data.success===1){
                        $('#p_'+id).val('0');
                        $('#quantity_'+id).html(data.quantity);
                    }else{
                        alert('Error');
                    }
                }
            });
        });
    }
    
    function afterGridUpdate(id, data){
        initButton();
    }
    
    function reloadGrid(data) {
        $.fn.yiiGridView.update('product-grid');
    }
</script>

<style>
    #preview {
        position: absolute;
        border: 1px solid #ccc;
        background: #333;
        padding: 5px;
        display: none;
        color: #fff;
    }

    #preview img {
        max-width: 500px;
        max-height: 500px;
    }
</style>